﻿using API.Model;
using Newtonsoft.Json;
using RestSharp;
using System.Net;
using System.Text;

namespace API.Logic
{
    public class CameraFunction
    {
        public CameraFunction()
        {

        }

        public void Token()
        {
            try
            {
                

                var model = CameraConfig();

                if (model != null && model.ExpireTime <= DateTime.Now)
                {
                    var url = new Uri("https://open.ys7.com/api/lapp/token/get");
                    {
                        var client = new RestClient(url);

                        var request = new RestRequest(url, Method.Post);

                        request.AddParameter("appKey", model.AppKey);

                        request.AddParameter("appSecret", model.AppSecret);

                        var response = client.Execute(request);

                        Console.WriteLine(response.Content);

                        if (response.StatusCode == HttpStatusCode.OK && response.Content != null)
                        {
                            var result = JsonConvert.DeserializeObject<dynamic>(response.Content);

                            if (result != null)
                            {
                                model.AccessToken = result.data.accessToken;

                                System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);

                                model.ExpireTime = dtDateTime.AddMilliseconds(Convert.ToDouble(result.data.expireTime)).ToLocalTime();

                                model.ExpireTime = model.ExpireTime.AddHours(-1);

                                var file = $"{Directory.GetCurrentDirectory()}/Hikvision.json";

                                using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
                                {
                                    using (StreamWriter sw = new StreamWriter(fs, new System.Text.UTF8Encoding(false)))
                                    {
                                        //开始写入  
                                        sw.Write(JsonConvert.SerializeObject(model));

                                        //清空缓冲区  
                                        sw.Flush();

                                        //关闭流  
                                        sw.Close();

                                        fs.Close();
                                    }
                                }
                            }

                        }


                    }

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }


        }


        public Hikvision CameraConfig()
        {
            var result = new Hikvision();

            var file = $"{Directory.GetCurrentDirectory()}/Hikvision.json";

            using (StreamReader reader = new StreamReader(file, Encoding.UTF8))
            {
                var content = reader.ReadToEnd().Trim();

                result = JsonConvert.DeserializeObject<Hikvision>(content);
            }
            return result;
        }
    }
}
